/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       skill_button.cpp

	$Header: /game/skill_button.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "skill_button.h"

#include "button.h"
#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "skill_icon.h"
#include "skill_properties.h"

extern t_bitmap_group_cache const   k_skill_frames( "button.skill_frame" );

void create_skill_button( t_button* button, t_skill const& skill, 
						  t_bitmap_group_ptr const& skill_frame_bitmaps_ptr, bool no_skill )
{
	t_bitmap_group_ptr		skill_icons_ptr;
	t_screen_rect			rect;
	t_bitmap_layer const*	layer;
	t_window*				window;
	t_window*				holder;
	t_screen_rect			icon_rect;
	
    /*
    ** Set skill icon offset.
    */
	icon_rect = skill_frame_bitmaps_ptr->find( "icon" )->get_rect();

	rect = t_screen_rect(0,0,0,0);
	/*
    ** Create the "released" image - first, create holder window:
    */
	layer = skill_frame_bitmaps_ptr->find( "released" );
	holder = new t_window( rect, t_window::k_completely_transparent, button );
    // Now, add skill image.
    if (!no_skill)
	    window = new t_skill_icon( skill, icon_rect, holder );
    // Add frame.
	window = new t_bitmap_layer_window( layer, t_screen_point(0,0), holder );
	holder->set_size_from_children();
	button->set_released_image( holder );

	/*
    ** Create the "pressed" image.
    */
	layer = skill_frame_bitmaps_ptr->find( "pressed" );
	holder = new t_window( rect, t_window::k_completely_transparent, button );
    if (!no_skill)
	    window = new t_skill_icon( skill, icon_rect, holder );
	window = new t_bitmap_layer_window( layer, t_screen_point(0,0), holder );
	holder->set_size_from_children();
	button->set_pressed_image( holder );
}

void create_frame_button( t_button* button, t_bitmap_group_ptr const& frame_bitmaps_ptr,
						  t_bitmap_group_ptr const& child_bitmaps_ptr, std::string const& child_name )
{
	t_screen_rect			rect;
	t_bitmap_layer const*	layer;
	t_window*				window;
	t_window*				holder;
	t_screen_rect			icon_rect;
	t_screen_point          offset;
	
    /*
    ** Set skill icon offset.
    */
	icon_rect = frame_bitmaps_ptr->find( "icon" )->get_rect();

	rect = t_screen_rect(0,0,0,0);
	/*
    ** Create the "released" image - first, create holder window:
    */
	holder = new t_window( rect, t_window::k_completely_transparent, button );

    // Now, add skill image.
	layer = child_bitmaps_ptr->find( child_name + "_released" );
	offset = -layer->get_rect().top_left();
	window = new t_bitmap_layer_window( layer, icon_rect.top_left() + offset, holder );
    
    // Add frame.
	layer = frame_bitmaps_ptr->find( "released" );
	window = new t_bitmap_layer_window( layer, t_screen_point(0,0), holder );
	holder->set_size_from_children();
	button->set_released_image( holder );

	/*
    ** Create the "pressed" image.
    */
	holder = new t_window( rect, t_window::k_completely_transparent, button );

	layer = child_bitmaps_ptr->find( child_name + "_pressed" );
	offset = -layer->get_rect().top_left();
	window = new t_bitmap_layer_window( layer, icon_rect.top_left() + offset, holder );

	layer = frame_bitmaps_ptr->find( "pressed" );
	window = new t_bitmap_layer_window( layer, t_screen_point(0,0), holder );
	holder->set_size_from_children();
	button->set_pressed_image( holder );
	
    /*
    ** Create the "highlighted" image.
    */
	holder = new t_window( rect, t_window::k_completely_transparent, button );

	layer = child_bitmaps_ptr->find( child_name + "_highlighted" );
	offset = -layer->get_rect().top_left();
	window = new t_bitmap_layer_window( layer, icon_rect.top_left() + offset, holder );

	layer = frame_bitmaps_ptr->find( "highlighted" );
	window = new t_bitmap_layer_window( layer, t_screen_point(0,0), holder );
	holder->set_size_from_children();
	button->set_pressed_image( holder );
}

// ---------------------------------------------------------------------------
// create a skill icon, and frame it
// ---------------------------------------------------------------------------
void create_framed_skill( t_skill const&			skill,
						  t_screen_point const&		point,
						  t_bitmap_group_ptr const& skill_frame_bitmaps_ptr,
						  t_window*					parent )
{
	t_screen_rect			icon_rect;
	t_bitmap_layer const*	layer;
	t_window*				window;

	icon_rect = skill_frame_bitmaps_ptr->find( "icon" )->get_rect();
	layer = skill_frame_bitmaps_ptr->find( "released" );
	window = new t_skill_icon( skill, icon_rect + point - icon_rect.top_left(), parent );
	window = new t_bitmap_layer_window( layer, point - icon_rect.top_left(), parent );
}
